#!/bin/sh

if [ "$MU_IP" = "" ] && [ "$#" -lt 2 ] || [ "$#" -lt 1 ] || [ $1 = "help" ]; then
  echo 1>&2 "Usage: $0 <action> <optional arg1> <optional arg2>
  where action is one of:
    newSession|closeSession <sessionId>|closeAllSessions|getAllSessions
    loadScenario <sessionId><uuid>
    setupTest <sessionId>|tearDownTest <sessionId>
    getHosts <sessionId>|getOptions <sessionId>|getChannels<sessionId>
    setHosts<sessionId><roles><names>|setOptions<sessionId><names><values>|setChannels<sessionId><roles><names>
    getTestSuiteResults <sessionId>|getTestSuiteStatus <sessionId>
    verifyScenario <sessionId>
    runTestSuite <sessionId><uuid>
    importCSV <sessionId><path_to_file.csv><filename>
    exportCSV <sessionId><uuid>

  "
  echo 1>&2 "You must set a permanant IP address: export MU_IP=<ip address>"
  echo 1>&2 "You must also export user and password: export MU_ADMIN_USER=<admin>; export MU_ADMIN_PASS=<password>"
  exit 127
fi

export command=$1
export value=$2
export value2=$3

if [ "$MU_ADMIN_USER" = "" ]; then
  export MU_ADMIN_USER="admin"
fi
if [ "$MU_ADMIN_PASS" = "" ]; then
  export MU_ADMIN_PASS="admin"
fi

#echo "command: $command"
#echo "value: $value"
#echo "value2: $value2"
#echo "value3: $value3"
#echo "ip set to: $MU_IP"
#echo "MU_ADMIN_USER = $MU_ADMIN_USER"
#echo "MU_ADMIN_PASS = $MU_ADMIN_PASS"

case $command in
  newSession)
    curl -X POST -k -c cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/newSession ;;
  closeAllSessions)
    curl -X POST -k -c cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/closeAllSessions ;;
  getAllSessions)
    curl -k -c cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/getAllSessions ;;
  closeSession)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/closeSession ;;
  loadScenario)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/loadScenario/${value} ;;
  getHosts)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/scenario/hosts ;;
  getChannels)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/scenario/channels ;;
  getOptions)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/scenario/options ;;
  setOptions)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/scenario/options/${value}/${value2} ;;
  setHosts)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/scenario/hosts/${value}/${value2} ;;
  setChannels)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/scenario/channels/${value}/${value2} ;;
  setupTest)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/setupTest ;;
  tearDownTest)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/test/tearDownTest ;;
  getTestSuiteResults)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/test/runSuite/results ;;
  getTestSuiteStatus)
    curl -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/test/runSuite/status ;;
  runTestSuite)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/test/runSuite/${value} ;;
  verifyScenario)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/test/run ;;
  importCSV)
    curl -F "file=@${value}" -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS  https://$MU_IP/api/v5/ddt/session/test/import/csv ;;
  exportCSV)
    curl -X POST -k -b cookie_jar --user $MU_ADMIN_USER:$MU_ADMIN_PASS https://$MU_IP/api/v5/ddt/session/test/export/csv/${value} ;;
  *)
    echo 1>&2 "unknown command - try $0 with no action for help"
    exit 127
    ;;
esac

